<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
define('API_URL', 'https://slog.slightech.com/report_data/add');
define('GET_KEY_URL', 'https://slog.slightech.com/report_item/get_item_key');
define('GET_SERVER_URL', 'https://slog.slightech.com/report_item/get_item_server');
require_once 'Base_fetch.php';
require_once 'Fan_base.php';

function generate_fetch_url() { 
	$item_server = Base_fetch::http_get(GET_SERVER_URL);
	$item_keys = Base_fetch::http_post(GET_KEY_URL, array('item_type' => 'mail_list', 'item_server' => $item_server));
	$key_arr = explode(" ", $item_keys);
	$fetch_url = array();
	foreach ($key_arr as $k => $row) {
		$fetch_key   = $row;
		$fetch_value = substr($row, 10,strlen($row));
		$fetch_url["$fetch_key"] = "http://www.jp.slightech.com/maillist/count/$fetch_value";
	}
	return $fetch_url;
}

run_fetch(generate_fetch_url());
run_all_method(new Fan_base());

function run_fetch($fetch_array) {
	foreach ($fetch_array as $key => $url) {
		$value = Base_fetch::http_get($url);
		$token = Base_fetch::generate_token($key, $value, date('Y-m-d'));
		Base_fetch::http_post(API_URL, array('key' => $key, 'value' => $value, 'token' => $token));
	}
}

function run_all_method($class) {
	$methods = get_class_methods($class);
	foreach ($methods as $method) {
		if (strpos($method, 'fetch') !== 0)
			continue;
		$values = $class->$method();
		foreach ($values as $key => $value) {
			$token = Base_fetch::generate_token($key, $value, date('Y-m-d'));
			Base_fetch::http_post(API_URL, array('key' => $key, 'value' => $value, 'token' => $token));
		}
	}
}
